//
//  GKAutorizationCodeItem.m
//  jinwan
//
//  Created by GKiOS on 2017/6/1.
//  Copyright © 2017年 wy. All rights reserved.
//

#import "GKAutorizationCodeItem.h"
#import "NSString+Library.h"
@interface GKAutorizationCodeItem ()
@property(nonatomic, strong) UITextField   *textField;
@property(nonatomic, strong) UILabel   *titleLabel;
@property(nonatomic, assign) CGFloat   maxWidth;
@end

@implementation GKAutorizationCodeItem

- (instancetype)initWithTitle:(NSString *)title titleMaxWidth:(CGFloat)maxWidth placeHolder:(NSString *)placeHolder
{
    self = [super init];
    if (self) {
        self.titleLabel.text = title;
        self.backgroundColor = [UIColor whiteColor];
        self.maxWidth = maxWidth;
        NSAttributedString *attri = [[NSAttributedString alloc] initWithString:placeHolder attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13], NSForegroundColorAttributeName:[UIColor custom_color:@"#CCCCCC"]}];
        self.textField.attributedPlaceholder = attri;
        [self addSubview:self.textField];
        [self addSubview:self.titleLabel];
        [self addSubview:self.timeButton];
        [self createConstraints];
        [self addNotification];
    }
    return self;
}

#pragma mark - constraints

- (void)createConstraints
{
//    CGSize size = [self.titleLabel.text sizeWithFont:[UIFont systemFontOfSize:14]];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.top.equalTo(self).offset(18);
        make.bottom.equalTo(self).offset(-18);
        make.width.equalTo(@(ceil(self.maxWidth)));
        make.height.greaterThanOrEqualTo(@0);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(32);
        make.centerY.equalTo(self);
        make.height.greaterThanOrEqualTo(@14);
    }];
    
    [self.timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-12);
        make.left.equalTo(self.textField.mas_right).offset(12);
        make.width.equalTo(@84);
    }];
}

#pragma mark - Notification

- (void)addNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:self.textField];
}

- (void)textFieldDidChange:(NSNotification *)noti
{
    UITextField *textField = noti.object;
    if (self.callBack) {
        self.callBack(textField.text);
    }
}

#pragma mark - setter

- (void)setKeyboardType:(UIKeyboardType)keyboardType
{
    _keyboardType = keyboardType;
    self.textField.keyboardType = keyboardType;
}

- (void)setSecureTextEntry:(BOOL)secureTextEntry
{
    _secureTextEntry = secureTextEntry;
    self.textField.secureTextEntry = _secureTextEntry;
}

#pragma mark - getter

- (UILabel *)titleLabel
{
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textColor = [UIColor custom_color:@"#333333"];
    }
    return _titleLabel;
}

- (UITextField *)textField
{
    if (_textField == nil) {
        _textField = [[UITextField alloc] init];
        _textField.textColor = [UIColor custom_color:@"#444444"];
        _textField.font = [UIFont systemFontOfSize:14];
    }
    return _textField;
}


- (UIButton *)timeButton
{
    if (_timeButton == nil) {
        _timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_timeButton setTitleColor:[UIColor custom_color:@"#A91E23"] forState:UIControlStateNormal];
        _timeButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [_timeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        _timeButton.layer.borderColor = [UIColor custom_color:@"#A91E23"].CGColor;
        _timeButton.layer.borderWidth = 1;
        _timeButton.layer.cornerRadius = 12;
        _timeButton.selected = YES;
        _timeButton.userInteractionEnabled = YES;
    }
    return _timeButton;
}

- (NSString *)content
{
    return self.textField.text;
}

#pragma mark - dealloc

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
